//
//  ViewController.swift
//  MonkeyPinch
//
//  Created by cygarm on 15/8/2.
//  Copyright (c) 2015年 cygarm. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    /*
    The UIPanGestureRecognizer passes itself as an argument to this function.
    */
    @IBAction func handlePan(recognizer: UIPanGestureRecognizer) {
        /*
        You can retrieve the amount the user has moved their finger by calling the translationInView: function.
        */
        let translation = recognizer.translationInView(self.view)
        if let view = recognizer.view {
            /*
            Here you use that amount to move the center of the monkey the same amount the finger has been dragged.
            */
            view.center = CGPoint(x: view.center.x + translation.x, y: view.center.y + translation.y)
        }
        /*
        It’s extremely important to set the translation back to zero once you are done. Otherwise, the translation will keep compounding each time, and you’ll see your monkey rapidly move off the screen!
        */
        recognizer.setTranslation(CGPointZero, inView: self.view)
        
        /*
        
        1.Figure out the length of the velocity vector (i.e. the magnitude)
        2.If the length is < 200, then decrease the base speed, otherwise increase it.
        3.Calculate a final point based on the velocity and the slideFactor.
        4.Make sure the final point is within the view’s bounds
        5.Animate the view to the final resting place.
        6.Use the “ease out” animation option to slow down the movement over time.

        */
        if recognizer.state == UIGestureRecognizerState.Ended {
            // 1
            let velocity = recognizer.velocityInView(self.view)
            let magnitude = sqrt((velocity.x * velocity.x) + (velocity.y * velocity.y))
            let slideMultiplier = magnitude / 200
            println("magnitude: \(magnitude), slideMultiplier: \(slideMultiplier)")
            
            // 2
            let slideFactor = 0.1 * slideMultiplier //Increse for more of a slide
            // 3
            var finalPoint = CGPoint(x: recognizer.view!.center.x + (velocity.x * slideFactor),
                                     y: recognizer.view!.center.y + (velocity.y * slideFactor))
            // 4
            finalPoint.x = min(max(finalPoint.x, 0), self.view.bounds.size.width)
            finalPoint.y = min(max(finalPoint.y, 0), self.view.bounds.size.height)
            
            // 5
            UIView.animateWithDuration(Double(slideFactor * 2),
                delay: 0,
                // 6
                options: UIViewAnimationOptions.CurveEaseOut,
                animations: {recognizer.view!.center = finalPoint},
                completion: nil)
        }
    }

    @IBAction func handlePinch(recognizer: UIPinchGestureRecognizer) {
        if let view = recognizer.view {
            view.transform = CGAffineTransformScale(view.transform, recognizer.scale, recognizer.scale)
            recognizer.scale = 1
        }
    }
    
    @IBAction func handleRotate(recognizer: UIRotationGestureRecognizer) {
        if let view = recognizer.view {
            view.transform = CGAffineTransformRotate(view.transform, recognizer.rotation)
            recognizer.rotation = 0
        }
    }
}

